<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class My_lab {

	var $CI;
    var $_model;
			
	public function __construct()
    {
      
        $CI =& get_instance();        
        $this->_model = $CI;
		$this->_model->load->database();
		$this->_model->load->model("muser");
		$this->_model->load->helper('date');		
    }
	function getLabSalary($Lab_ID,$OverTime_ID,$FromDate,$ToDate){
		$result=$this->_model->muser->getStaffInLab($Lab_ID);
		if ($result=='0') return FALSE;
		$over=$this->_model->muser->getOT($OverTime_ID);	
		if ($over=='0') return FALSE;
		foreach ($result as $row) {
			$Staff_ID=$row->Staff_ID;
			$basic=$row->BasicSalary;
			$basic= str_replace(",", "",$basic);
			
			$time=$this->_model->muser->getRMonthTime($Staff_ID,$FromDate,$ToDate);
			
			$h=$basic/$time->WorkingDay/8;
			
			$salary1=$basic*($time->WorkedDay+$time->T7)/$time->WorkingDay;
			$salary2=$this->getOT($over, $time, $h);
			$data=array(
					"Staff_ID"=>$Staff_ID,
					"Lab_ID"=>$Lab_ID,
					"FromDate"=>$FromDate,
					'ToDate'=>$ToDate,
					"BasicSalary"=>number_format($basic),
					"ActSalary"=>number_format($salary1),
					"OTSalary"=>number_format($salary2),
					"PaySalary"=>number_format($salary1+$salary2)			
			);
			$r=$this->_model->muser->checkCustomerReport($Lab_ID,$Staff_ID,$FromDate,$ToDate);
			if ($r=='0'){
				$this->_model->muser->addData('CustomerReport',$data);	
			}else{
				$this->_model->muser->updateData('CustomerReport','CustomerReport_ID',$r->CustomerReport_ID,$data);
			}
						
			
		}
		 return TRUE;
		
	}
	
	function makeLabPercent($Lab_ID,$Percent,$FromDate,$ToDate){
		$result=$this->_model->muser->getStaffInLab($Lab_ID);
		if ($result=='0') return FALSE;
		foreach ($result as $row){
			$Staff_ID=$row->Staff_ID;
			$result=$this->_model->muser->getStaffSalary($Staff_ID,$FromDate,$ToDate);
			if ($result=='0') continue;
			
			$basic=$result->BasicSalary;
			$basic=str_replace(",","", $basic);
			$total=$result->Total;
			$total=str_replace(",","", $total);
			
			
			$data=array(
					"Staff_ID"=>$Staff_ID,
					"Lab_ID"=>$Lab_ID,
					"FromDate"=>$FromDate,
					'ToDate'=>$ToDate,
					"ActSalary"=>'0',
					"OTSalary"=>'0',
					"BasicSalary"=>number_format($basic *($Percent+1)),					
					"PaySalary"=>number_format($total*($Percent+1))			
			);
			$r=$this->_model->muser->checkCustomerReport($Lab_ID,$Staff_ID,$FromDate,$ToDate);
			if ($r=='0'){
				$this->_model->muser->addData('CustomerReport',$data);	
			}else{
				$this->_model->muser->updateData('CustomerReport','CustomerReport_ID',$r->CustomerReport_ID,$data);
			}
			
		}
		return TRUE;
		
	}

	function getOT($over,$thoigian,$h){
		 $c=  $h*( $thoigian->OT_Weekday*($over->OT_Weekday) + 
		 	$thoigian->OT_Saturday*($over->OT_Saturday)+
		 	$thoigian->Time_Saturday*($over->Time_Saturday)+
		 	$thoigian->OT_Sunday*($over->OT_Sunday)+
		 	$thoigian->OT_Holiday*($over->OT_Holiday)+
		 	$thoigian->ON_Weekday*($over->ON_Weekday)+
		 	$thoigian->ON_Weekend*($over->ON_Weekend)+
		 	$thoigian->ON_Holiday*($over->ON_Holiday))/100;  //so tien khong phai chiu thue
		return $c;
		
	}
  


}
/* End of file Someclass.php */